import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Vector;

import javax.naming.NamingException;

import unilearn.domain.Assessment;
import unilearn.domain.Assignment;
import unilearn.domain.Submission;
import unilearn.facade.AssessmentFacade;
import unilearn.facade.AssignmentFacade;
import unilearn.facade.SubmissionFacade;

public class UseStories2
{
	private PrintWriter out;

	public UseStories2() throws SQLException, FileNotFoundException
	{
		out = new PrintWriter(new FileOutputStream("UserStories2.txt"));
	}

	public void close()
	{
		try
		{
			out.close();
		}
		finally
		{
			out = null;
		}
	}

	@SuppressWarnings("deprecation")
	public void printStudents() throws SQLException, NamingException
	{
		String[] students = SubmissionFacade.getStudentNumbers();
		for (String sub : students)
		{
			out.println(sub);
		}
	}

	public void printSubmissions(String studentNumber) throws SQLException, NamingException
	{
		out.print("Student ID: ");
		out.println(studentNumber);
		out.println();

		Vector<Submission> submissions = SubmissionFacade.getSubmissions(studentNumber);
		for (Submission sub : submissions)
		{
			printSubmission(sub);
		}

		out.println();
	}

	private void printSubmission(Submission sub) throws SQLException, NamingException
	{
		Assignment ass;
		ass = AssignmentFacade.getAssignmentBySubmission(sub);
		long subjectID = ass.getSubjectID();

		out.print(". <Subject (");
		out.print(subjectID);

		out.print(")> | ");

		out.print(ass.getName());

		out.print(" | Grade: ");
		Assessment ases = AssessmentFacade.getBySubmissionID(sub.getSubmissionID());
		if (ases == null)
		{
			out.println("\t UN-ASSESSED");
		}
		else
		{
			out.println(ases.getGrade());
		}
	}

	public static void main(String[] args) throws SQLException, FileNotFoundException,
		NamingException
	{
		UseStories2 test = new UseStories2();
		try
		{
			if (args.length != 1)
			{
				test.printStudents();
			}
			else
			{
				test.printSubmissions(args[0]);
			}
		}
		finally
		{
			test.close();
		}

	}
}
